package com.cnc.ssm.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
 
import com.cnc.bas.util.Constants;
import com.cnc.bas.util.LTUtil;
import com.cnc.bas.util.MD5PasswordEncoder;
import com.cnc.bas.util.SpringBeanFactory;
import com.cnc.bas.web.BasMapForm;
import com.cnc.bas.web.SecureBaseDispatchAction;
import com.cnc.fd.service.FdDao;
import com.cnc.fd.sqlmapDao.ICustomerConfDao;
import com.cnc.fd.util.FtpUtil;
import com.cnc.fd.util.QueryOpersHelper;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.model.PwOper;
import com.cnc.ssm.model.PwRole;


/**
 * User: Du bf
 * Date: 2005-3-24
 */

public class OperAction extends SecureBaseDispatchAction {
    private ISsmService ssmService = null;
    private ICustomerConfDao customerConfDao = null;
    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        ssmService = (ISsmService) this.getBean(Constants.BEAN_NAME_SSM);
        customerConfDao = (ICustomerConfDao) this.getBean(ICustomerConfDao.BEAN_NAME);
    }

    public ActionForward search(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
    	//liuwei修改过
        request.setAttribute("listEmplType", ssmService.listCodeInfo(Constants.CODE_TYPE_0021));
        return mapping.findForward("search");
    }

    public ActionForward list(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HashMap hs = ((BasMapForm) form).getHs();
        LTUtil.addHsLike(hs, "operCode,operName");
        List list = ssmService.listOperator(hs);
        LTUtil.setPageData(request, list);
        return mapping.findForward("list");
    }

    public ActionForward add(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
    	PwOper oper = getEncoderOper(form, request,true);
        ssmService.addOperator(oper);
        // 刷新缓存
        QueryOpersHelper.getInstance().refresh(oper.getOperCode());
        
        if (PwOper.OPER_TYPE_CUSTOMER.equals(oper.getOperType())) {
			customerConfDao.addCustomerConfDefaut(oper.getOperCode());
			
			// 添加customer角色
	        ssmService.saveRoleOper("O", PwRole.COMMON_ROLE_CODE, oper.getOperCode());
		}
        //创建FTP用户
        if("0".equals(oper.getUploadKind())){
        	
        	FdDao fdDao = (FdDao)SpringBeanFactory.getBean(FdDao.BEAN_ID);
    		Map server = fdDao.getUploadServerByCustomer(oper.getOperCode());	
    		if(server != null){
    			String hostIp = String.valueOf(server.get("serverIp"));
        		String sshUsr = String.valueOf(server.get("sshUser"));
        		String sshPwd = String.valueOf(server.get("sshPwd"));
        		
        		int sshPort ;		
        		
        		try{
        			 sshPort = Integer.parseInt(server.get("sshPort").toString());
        		}catch(RuntimeException e){
        			 sshPort = 57891;
        		}
        		FtpUtil.createFtpUser(hostIp, sshUsr, sshPwd, sshPort, oper.getOperCode());
    		}
    		   		
        }
        
        return mapping.findForward("edit");
    }

    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
    	PwOper oper = getEncoderOper(form, request,false);
        ssmService.updateOperator(oper);
        
        // 刷新缓存
        QueryOpersHelper.getInstance().refresh(oper.getOperCode());
        return mapping.findForward("edit");
    }

	private PwOper getEncoderOper(ActionForm form, HttpServletRequest request,boolean isAdd) {
		String pwd = request.getParameter("pwOper.operPwd");
		String oldpwd = request.getParameter("oldPwd");
    	PwOper oper = ((OperForm) form).getPwOper();
    	boolean isEncoder = false;
    	if(!isAdd){//修改状态且密码有做修改
    		if(!pwd.equals(oldpwd)){
    			isEncoder = true;
    		}
    	}
    	if(isEncoder || isAdd){
    		MD5PasswordEncoder encoder = new MD5PasswordEncoder();
    		oper.setOperPwd(encoder.encode(pwd));
    	}
		return oper;
	}

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        ssmService.deleteOperator(id);
        return mapping.findForward("list");
    }

    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        PwOper obj = (PwOper) ssmService.loadOperator(id);
        ((OperForm) form).setPwOper(obj);
        return mapping.findForward("edit");
    }

    public ActionForward create(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        PwOper oper = new PwOper();
        oper.setOperType(PwOper.OPER_TYPE_CUSTOMER);
        oper.setUploadKind(PwOper.UPLOAD_KIND_FTP);
        ((OperForm) form).setPwOper(oper);
        return mapping.findForward("edit");
    }

    public ActionForward changePassword(ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        BasMapForm fm = (BasMapForm) form;
        ssmService.changePwd(fm.getHs());
        return mapping.findForward("changePasswordStatus");
    }

    public ActionForward changePasswordInit(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        BasMapForm fm = (BasMapForm) form;
        ssmService.changePwd(fm.getHs());
        return mapping.findForward("main");
    }

    public ActionForward fwChangePwd(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        return mapping.findForward("changePassword");
    }

    public ActionForward setOperRole(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        String opercode = request.getParameter("opercode");
        String rolelist = request.getParameter("rolelist");
        ssmService.saveRoleOper("O", rolelist, opercode);
        return mapping.findForward("saveList");
    }

    public ActionForward getOperAuth(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        String opercode = request.getParameter("opercode");
        List list = ssmService.listAuth("O", opercode);
        request.setAttribute("list", list);
        return mapping.findForward("authList");
    }

    public ActionForward setOperAuth(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        String opercode = request.getParameter("opercode");
        String authlist = request.getParameter("authlist");
        ssmService.saveAuth("O", opercode, authlist);
        return mapping.findForward("saveList");
    }   
    
}
